package com.ttn.hire.bus;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;

import com.ttn.entities.CarHire;
import com.ttn.entities.Connection;
import com.ttn.entities.User;
import com.ttn.search.bus.SearchBean;

@Service
public class HireService extends HibernateDaoSupport {
	
	@Autowired
	public void init(SessionFactory factory) {
	    setSessionFactory(factory);
	}
	
	public CarHire createEmptyHire(User user){
		CarHire carHire = new CarHire();
		carHire.setUser(user);
		return carHire;		
	}
	
	public void saveHire(CarHire hire){
		this.getHibernateTemplate().save(hire)	;		
	}
	
	
	

	public List<CarHire> search(SearchHireBean command){
		Criteria criteria = this.getSession().createCriteria(CarHire.class);
		criteria.add(Restrictions.eq("car", command.getCar()));
			
		/*
		if (command.getTripDate() != null){
			criteria.add(Restrictions.eq("tripDate", command.getTripDate()));
		}else{
			criteria.add(Restrictions.gt("tripDate", new Date()));	
		}*/
		
		return criteria.list();
	}
	
	
	public SearchHireBean createEmptySearchBean(){
		return new SearchHireBean();
	}
	
	
	
	

}
